package com.lzy.cloud.goods.controller;

import com.lzy.cloud.goods.dtos.GoodsAppDto;
import com.lzy.cloud.goods.dtos.GoodsDto;
import com.lzy.cloud.goods.facade.GoodsFacade;
import com.lzy.cloud.goods.facade.ParamFacade;
import com.lzy.cloud.goods.remote.BaseApi;
import com.lzy.cloud.goods.remote.response.UserToken;
import com.lzy.cloud.goods.request.GoodsAppRequest;
import com.lzy.cloud.goods.request.GoodsRequest;
import com.lzy.cloud.goods.response.GoodsAppResponse;
import com.lzy.cloud.goods.response.GoodsResponse;
import com.lzy.cloud.goods.service.GoodsService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2018/6/6.
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    private GoodsFacade goodsFacade;

    @Autowired
    private BaseApi baseApi;

    /**
     * 新增商品
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result addGoods(@RequestBody GoodsRequest request) {
        Ensure.that(request).isNotNull("00010001");
//        UserToken userToken = DozerUtils.map(baseApi.isUserLogin(token).getResult(),UserToken.class);
//        request.setCreateUser(userToken.getId());
//        Boolean ok = goodsFacade.addGoods(DozerUtils.map(request, GoodsDto.class),userToken.getPlatformId());
        Boolean ok = goodsFacade.addGoods(DozerUtils.map(request, GoodsDto.class),1L);
        Ensure.that(ok).isTrue("00010002");
        return new Result(ok);
    }

    /**
     * 根据id批量删除商品
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del/{mode}/{ids}", method = RequestMethod.GET)
    public Result delGoods(@RequestHeader("token") String token,@PathVariable("mode") Integer mode,@PathVariable("ids") String ids) {
        Ensure.that(ids).isNotNull("00010003");
        Boolean ok = goodsFacade.del(mode,ids);
        Ensure.that(ok).isTrue("00010004");
        return new Result(ok);
    }

    /**
     *  批量恢复到仓库
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/recovery/{mode}/{ids}",method = RequestMethod.GET)
    public Result recoveryById(@RequestHeader("token") String token,@PathVariable("mode") Integer mode,@PathVariable("ids") String ids){
        Ensure.that(ids).isNotNull("00010001");
        Boolean ok = goodsFacade.recoveryById(mode, ids);
        Ensure.that(ok).isTrue("00010004");
        return new Result(ok);
    }

    /**
     * 根据id修改商品
     *@RequestHeader("token") String token,
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody GoodsRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = goodsFacade.update(DozerUtils.map(request, GoodsDto.class));
        Ensure.that(ok).isTrue("00010005");
        return new Result(ok);
    }

    /**
     * 根据id查询商品详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/find/{mode}/{id}", method = RequestMethod.GET)
    public Result selectById(@PathVariable("mode") Integer mode,@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010003");
        GoodsResponse goodsDto = goodsFacade.selectById(mode,id);
        return new Result(goodsDto);
    }

    /**
     * 查询商品列表(分页)查子表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result selectList(GoodsRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        Result result = goodsFacade.selectList(pager, DozerUtils.map(request, GoodsDto.class),request.getSortType());
        return result;
    }

    /**
     * 共享至供销平台
     * @param mode
     * @param id
     * @return
     */
    @RequestMapping(value = "/share/{mode}/{id}", method = RequestMethod.GET)
    public Result addToShare(@RequestHeader("token") String token,@PathVariable("mode") Integer mode,@PathVariable("id") String id) {
        Ensure.that(id).isNotNull("00010003");
        Ensure.that(mode).isNotNull("00010003");
        Boolean ok = goodsFacade.addToShare(mode,id);
        Ensure.that(ok).isTrue("00010004");
        return new Result(ok);
    }

    /**
     * 移除供销平台
     * @param mode
     * @param id
     * @return
     */
    @RequestMapping(value = "/noshare/{mode}/{id}", method = RequestMethod.GET)
    public Result removeShare(@RequestHeader("token") String token,@PathVariable("mode") Integer mode,@PathVariable("id") String id) {
        Ensure.that(id).isNotNull("00010003");
        Ensure.that(mode).isNotNull("00010003");
        Boolean ok = goodsFacade.removeShare(mode,id);
        Ensure.that(ok).isTrue("00010004");
        return new Result(ok);
    }

    /**
     * 从回收站中删除数据,真删
     * @param mode
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{mode}/{id}", method = RequestMethod.GET)
    public Result deleteByKey(@RequestHeader("token") String token,@PathVariable("mode") Integer mode,@PathVariable("id") String id) {
        Ensure.that(id).isNotNull("00010003");
        Ensure.that(mode).isNotNull("00010003");
        Boolean ok = goodsFacade.deleteByKey(mode,id);
        Ensure.that(ok).isTrue("00010004");
        return new Result(ok);
    }

    /**
     * 根据id编辑角色
     *
     * @param request
     * @return
     */
//    @RequestMapping(value = "/update/id", method = RequestMethod.POST)
//    public Result updateById(@RequestHeader("token") String token,@RequestBody GoodsRequest request) {
//        Ensure.that(request).isNotNull("00010001");
//        Ensure.that(request.getId()).isNotNull("00010003");
//        Boolean ok = goodsService.updateById(DozerUtils.map(request, GoodsDto.class));
//        Ensure.that(ok).isTrue("00010005");
//        return new Result(ok);
//    }

    /**
     * 根据code编辑商品
     *
     * @param request
     * @return
     */
//    @RequestMapping(value = "/update/code", method = RequestMethod.POST)
//    public Result updateByCode(@RequestHeader("token") String token,@RequestBody GoodsRequest request) {
//        Ensure.that(request).isNotNull("00010001");
//        Ensure.that(request.getCode()).isNotNull("00010006");
//        Boolean ok = goodsService.updateByCode(DozerUtils.map(request, GoodsDto.class));
//        Ensure.that(ok).isTrue("00010005");
//        return new Result(ok);
//    }

    /**
     * 根据code查询商品
     *
     * @param code
     * @return
     */
//    @RequestMapping(value = "/select/code/{code}", method = RequestMethod.GET)
//    public Result selectByCode(@PathVariable("code") String code) {
//        Ensure.that(code).isNull("00010006");
//        GoodsDto goodsDto = goodsService.selectByCode(code);
//        return new Result(DozerUtils.map(goodsDto, GoodsResponse.class));
//    }

    /**
     * 根据type查询商品列表(分页)
     *
     * @param type
     * @return
     */
//    @RequestMapping(value = "/select/type/{type}", method = RequestMethod.GET)
//    public Result selectByType(@PathVariable("type") Integer type, GoodsRequest request) {
//        Ensure.that(type).isNotNull("00010008");
//        Ensure.that(request).isNotNull("00010001");
//        Pager pager = new Pager();
//        pager.setPageNum(request.getPageNum());
//        pager.setPageSize(request.getPageSize());
//        PageInfo<GoodsDto> pageInfo = goodsService.selectListByType(pager, type);
//        if (pageInfo != null && pageInfo.getList() != null) {
//            pager.setTotal(pageInfo.getTotal());
//            return new Result(pager, DozerUtils.maps(pageInfo.getList(), GoodsResponse.class));
//        } else {
//            return new Result();
//        }
//    }

    /************************app端**************************/
    /**
     * 查询商品列表(分页)查子表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/app/list", method = RequestMethod.GET)
    public Result findList(GoodsAppRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        Result result = goodsFacade.findList(pager, DozerUtils.map(request, GoodsAppDto.class),request.getSortType());
        return result;
    }

    /**
     * 根据id查询商品详情
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/app/find/{mode}/{id}", method = RequestMethod.GET)
    public Result findById(@PathVariable("mode") Integer mode,@PathVariable("id") Long id) {
        Ensure.that(id).isNotNull("00010003");
        GoodsAppResponse goodsDto = goodsFacade.findById(mode,id);
        return new Result(goodsDto);
    }

}
